package directions;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Jeff
 *
 *	Directions menu Class
 * 	Prints directions for all item combinations
 */
public class DirectionsMenu {

	public String menuChoice = "";

	public DirectionsMenu(String menuChoice)
	{
		this.menuChoice = menuChoice;
		menuString();
	}



	/**
	 * Prints directions option screen
	 */
	private void menuString()
	{
		String defaultTitle = "Your Order Is:,";	
		if (menuChoice.length() > defaultTitle.length()){
			ArrayList<String> choices = new ArrayList<String>(); 
			printDirections(menuChoice);
		}
		menuChoice = "";
	}



	// TODO handle multiple orders


	/**
	 * @param order
	 * 
	 * takes in a string and turns it into a linked list
	 * compares strings to create the right directions output
	 */
	public void printDirections(String order)
	{
		LinkedList<String> orderList = new LinkedList<String>();
		LinkedList<String> args = new LinkedList<String>();
		String itemMatch;

		int spaceIndex = order.indexOf("\n");
		String rest = order.substring(spaceIndex);
		rest = rest.trim();

		for(String arg : rest.trim().split("\n"))
			orderList.add(arg);

		//put each order in a linked list
		//pop each order and run it through the main loop
		//which takes in one node and turns that into another linked list
		//keep going til order linked list is done


		//		System.out.println(orderList.size());

		// TODO
		// this doesn't work, but it's the basic idea (maybe?)
		// you want to loop your print directions until you 
		// have nothing more to parse.  so after you print 1 direction, you 
		// should have stuff left over in your args string.
		while(orderList.size() != 0){
			rest = orderList.pop();


			for(String arg : rest.trim().split(","))
				args.add(arg);

			itemMatch = args.pop();

			if(itemMatch.equals("Drink"))
			{
				int stepNum = 3;
				String drinkType = args.pop();
				@SuppressWarnings("unused")
				String price = args.pop();
				String checkType = args.pop();
				String cupSize = null;
				String flavorType = null;
				LinkedList<String> toppingList = new LinkedList<String>();

				//If flavor is first then size is last.
				if(checkType.equals("Flavor"))
				{
					flavorType = args.pop();
					args.pop();
					cupSize = args.pop();
				}

				if(checkType.equals("Size"))
				{
					cupSize = args.pop();
					args.pop();
					flavorType = args.pop();
				}

				if(checkType.equals("Cream") || checkType.equals("Sugar") || checkType.equals("Iced"))
				{
					toppingList.push(checkType);
					if(args.size() != 0)
					{
						toppingList.push(args.pop());
					}
				}

				if(drinkType.equals("Coolatta") || drinkType.equals("Smoothie") || drinkType.equals("Milkshake"))
				{
					System.out.println("How to make a " + flavorType + " " + drinkType + ":");
					System.out.println("1. Retrieve " + cupSize + " cup.");
					System.out.println("2. Pour " + flavorType + " " + drinkType + " mix into blender.");
					System.out.println("3. Blend.");
					System.out.println("4. Pour " + flavorType + " " + drinkType + " into cup.");
				}

				// TODO coffee with multiple toppings

				if(drinkType.equals("Fountain Drink"))
				{
					System.out.println("How to make a " + flavorType + " " + drinkType + ":");
					System.out.println("1. Retrieve " + cupSize + " cup.");
					System.out.println("2. Pour " + flavorType + " " + drinkType + " into cup.");
				}

				if(drinkType.equals("Coffee"))
				{
					//run topping stuff
					System.out.println("1. Retrieve " + drinkType + " cup.");
					System.out.println("2. Pour " + drinkType + " into cup.");
					printToppings(toppingList, stepNum, drinkType);
				}

			}

			if(itemMatch.equals("Hot Food"))
			{
				String foodType = args.pop();
				@SuppressWarnings("unused")
				String price = args.pop();

				if(foodType.equals("Apple Pie") || foodType.equals("Danishes") || foodType.equals("Hash Browns")
						|| foodType.equals("Apple Fries"))
				{
					System.out.println("How to prepare " + foodType + ":");
					System.out.println("1. Retrieve " + foodType + " from freezer.");
					System.out.println("2. Place " + foodType + " in microwave for 5 minutes.");
					System.out.println("3. Remove " + foodType + " from microwave and place it on a plate.");
				}

				else
				{
					System.out.println("How to prepare a " + foodType + " sandwich:");
					System.out.println("1. Retrieve two slices of bread.");
					System.out.println("2. Place " + foodType + " between bread.");
					System.out.println("3. Place " + foodType + " sandwich onto plate.");
				}
			}

			if(itemMatch.equals("Bakery"))
			{
				String foodType = args.pop();
				@SuppressWarnings("unused")
				String price = args.pop();


				if(foodType.equals("Donut"))
				{
					String flavorType = args.pop();
					System.out.println("How to prepare a " + flavorType + " Donut:");
					System.out.println("1. Retrieve " + flavorType + " Donut from basket");
					System.out.println("2. Place Donut on plate.");
				}

				if(foodType.equals("Bagel"))
				{
					int stepNum = 2;
					System.out.println("How to prepare a " + foodType + ":");
					System.out.println("1. Retrieve Bagel from basket.");
					String toppingType = args.pop();

					while(toppingType.equals("Butter") || toppingType.equals("Jam") || toppingType.equals("Cream Cheese"))
					{
						System.out.println(stepNum + ". Spread " + toppingType + " inbetween bagel slices.");
						toppingType = args.pop();
						stepNum++;
						if(args.size() == 0)
						{
							break;
						}
						else
						{
							toppingType = args.pop();
						}

					}
					System.out.println(stepNum + ". Place Bagel on plate.");
				}

				if(foodType.equals("Muffin"))
				{
					String muffinType = args.pop();
					System.out.println("How to prepare a " + muffinType + " " + foodType + ":");
					System.out.println("1. Retrieve " + muffinType + " muffin from basket.");
					System.out.println("2. Place muffin on plate.");
				}
			}

			if(itemMatch.equals("Ice Cream"))
			{
				int stepNum = 3;
				String styleType = null;
				String containerType = null;
				String flavorType = null;
				LinkedList<String> toppingList = new LinkedList<String>();

				String userChoice = args.pop();

				while(args.size() != 0)
				{
					if(userChoice.equals("Style"))
					{
						styleType = args.pop();
						System.out.println(styleType);
						args.pop(); //pop for price
						if(args.size() != 0)
						{
							userChoice = args.pop();
						}
					}
					if(userChoice.equals("Container"))
					{
						containerType = args.pop();	
						System.out.println(containerType);

						if(containerType.equals("Cone"))
						{
							String coneType = args.pop();
							containerType = coneType + " Cone";
							args.pop(); //pop for price
						}

						if(containerType.equals("Bowl"))
						{
							String bowlType = args.pop();
							containerType = bowlType + " Bowl";

							if(bowlType.equals("Medium") || bowlType.equals("Large"))
							{
								args.pop(); //pop for price
							}
						}

						if(args.size() != 0)
						{
							userChoice = args.pop();
						}
					}
					if(userChoice.equals("Flavor"))
					{
						flavorType = args.pop();
						System.out.println(flavorType);
						if(args.size() != 0)
						{
							userChoice = args.pop();
						}
					}
					if(userChoice.equals("Toppings"))
					{
						String toppingType = args.pop();

						while(toppingType.equals("Crushed Peanuts") || toppingType.equals("Sprinkles") || toppingType.equals("Maraschino Cherries")
								|| toppingType.equals("Chocolate Syrup") || toppingType.equals("Whipped Cream"))
						{
							//System.out.println("pushing " + toppingType);
							toppingList.push(toppingType);
							args.pop();  //pop for price


							if(args.size() != 0)
							{
								toppingType = args.pop();
							}
							else
							{
								break;
							}

							if(toppingType.equals("Flavor") || toppingType.equals("Container") 
									|| toppingType.equals("Style"))
							{
								userChoice = toppingType;
								break;
							}
						}
					}
					//System.out.println(args.size());
				}
				System.out.println("How to prepare " + flavorType + " ice cream:");
				System.out.println("1. Retrieve " + containerType + ".");

				if(styleType.equals("Soft Served"))
				{
					System.out.println("2. Pull handle on " + flavorType + " soft serve machine and put it into " + containerType + ".");
				}

				if(styleType.equals("Scooped"))
				{
					System.out.println("2. Scoop " + flavorType + " and put it into " + containerType + ".");
				}

				printToppings(toppingList, stepNum, itemMatch);
			}


			if(itemMatch.equals("Sundae"))
			{
				int stepNum = 3;
				args.pop(); //pop the price
				String checkType = args.pop();
				String flavorType = null;
				String premiumType = null;
				String classType = null;
				LinkedList<String> toppingsList = new LinkedList<String>();
				String toppingType = null;

				while(args.size() != 0)
				{
					if(checkType.equals("Toppings"))
					{
						toppingType = args.pop();

						while(toppingType.equals("Crushed Peanuts") || toppingType.equals("Sprinkles") || toppingType.equals("Maraschino Cherries")
								|| toppingType.equals("Chocolate Syrup") || toppingType.equals("Whipped Cream"))
						{
							//System.out.println("pushing " + toppingType);
							toppingsList.push(toppingType);
							args.pop();  //pop for price


							if(args.size() != 0)
							{
								toppingType = args.pop();
							}

							else
							{
								break;
							}

							if(toppingType.equals("Type"))
							{
								checkType = toppingType;
							}
						}

					}

					if(checkType.equals(("Type")))
					{
						classType = args.pop();

						if(classType.equals("Premium"))
						{
							flavorType = "Vanilla";
							//extra steps need to be printed
							premiumType = args.pop();
							if(premiumType.equals("Banana Split"))
							{
								premiumType = "Bananas";
							}
							if(premiumType.equals("Oreo Layered"))
							{
								premiumType = "Oreo Crumbs";
							}
						}

						else
						{
							flavorType = args.pop();
						}

						if(args.size() != 0)
						{
							checkType = args.pop();
						}
					}
				}

				System.out.println("How to prepare a " + classType + " sundae:");
				System.out.println("1. Retrieve sundae bowl.");
				System.out.println("2. Scoop " + flavorType + " ice cream and put in bowl.");
				if(premiumType != null)
				{
					System.out.println("3. Place " + premiumType + " on top of ice cream.");
					stepNum++;
				}
				stepNum = printToppings(toppingsList, stepNum, itemMatch);
				System.out.println(stepNum + ". Put spoon in bowl.");
			}
			System.out.println();
		}
	}

	/**
	 * @param args
	 * @param stepNum
	 * @param itemType
	 * @return stepNum
	 * 
	 * Prints toppings based on item type
	 */
	public int printToppings(LinkedList<String> args, int stepNum, String itemType)
	{
		String toppingType = null;

		if(itemType.equals("Ice Cream") || itemType.equals("Sundae"))
		{
			while(args.size() != 0)
			{
				toppingType = args.pop();
				if(toppingType.equals("Crushed Peanuts") || toppingType.equals("Sprinkles"))
				{
					System.out.println(stepNum + ". Sprinkle " + toppingType + " over ice cream.");
				}

				if(toppingType.equals("Maraschino Cherries") || toppingType.equals("Chocolate Syrup")
						|| toppingType.equals("Whipped Cream"))
				{
					System.out.println(stepNum + ". Place " + toppingType + " on top of ice cream.");
				}
				stepNum++;


			}
		}

		if(itemType.equals("Coffee"))
		{
			while(args.size() != 0)
			{
				toppingType = args.pop();
				System.out.println(stepNum + ". Pour " + toppingType + " into coffee.");
				stepNum++;
			}
			System.out.println(stepNum + ". Stir and put cover on cup.");
		}

		return stepNum;

	}

	/**
	 * @param choices menu choices
	 * @return valid choice
	 */
	protected int presentMenu(List<String> choices){
		Scanner in = new Scanner(System.in);
		StringBuffer sb = new StringBuffer();

		System.out.println("\nPlease enter an option:");
		for(int i=0; i<choices.size(); i++)
			sb.append(i).append(") ").append(choices.get(i)).append("\n");

		int choice = -1;
		boolean first = true;
		while(choice == -1){
			if(!first)
				System.out.println("\nThis is not a valid choice, try again.\n");
			first = false;

			System.out.println(sb.toString());

			try{
				choice = Integer.parseInt(in.nextLine());
			} catch (NumberFormatException nfe) {
				choice = -1;
			}


			if(choice < 0 || choice >= choices.size())
				choice = -1;
		}
		return choice;
	}	
}
